package de.listrong.da.tools;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class NewIDGener {
	public static String COUNT_FILE_PATH = "E:\\workspace34\\obit\\configurations\\count.txt";
	private static Properties idProperties = new Properties();

    public static int newId(String idLabel) {
		loadIdList();
		
		int id;
		if (!idProperties.containsKey(idLabel)) {
			idProperties.setProperty(idLabel, "1");
			id = 1;
		} else {
			id = Integer.valueOf(idProperties.getProperty(idLabel));
			idProperties.setProperty(idLabel, String.valueOf(++id));
		}
		
		saveIdList();
		return id;
	}

	private static void loadIdList() {
		try {
			idProperties.load(new FileInputStream(COUNT_FILE_PATH));
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("Error By loading id" + e);
		}
	}

	private static void saveIdList() {
		try {
			idProperties.store(new FileOutputStream(COUNT_FILE_PATH), "Auto Saved ID counter");
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("Error By save id" + e);
		}
	}


}
